<template>
  <div class='container'>
   <!-- 卡片视图 -->
  <el-card class="box-card">

  <div slot="header" class="clearfix">
<el-row>
  <el-col :span="6">
    <el-form :model="queryInfo" label-width="80px" >
  <el-form-item label="关键字">
    <el-input v-model='queryInfo.keyword' placeholder="根据编号搜索"></el-input>
  </el-form-item>
    </el-form>
  </el-col>
  <el-col :span="18">
     <el-button style="float: right; margin:0 8px" type="primary" size="small" @click="list">搜索</el-button>
     <el-button style="float: right;" size="small"  @click="clearKeyword">清除</el-button>
    </el-col>
</el-row>
<!-- 数据提示 -->
 <el-alert
    :title= "`数据一共${total}条`"
    type="info"
    show-icon>
  </el-alert>
  </div>
<!-- 表格区域 -->
  <el-table
      :data="listItem"
      style="width: 100%">

      <el-table-column
        prop="id"
        label="编号"
        width="200">
      </el-table-column>
      <el-table-column
        prop="questionType"
        label="题型"
        width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.questionType === '1'">单选</span>
          <span v-else-if="scope.row.questionType === '2'">多选</span>
          <span v-else>简答</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="questionIDs"
        label="题目编号"
        width="190px"
        >
        <template slot-scope="scope">
          <div v-for="(item,index) in scope.row.questionIDs"
          :key="index"
          style="color: rgb(0,153,255);"
          @click="getItemDialog(item)"
          >
          {{item.number}}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="addTime"
        label="录入时间">
      </el-table-column>
      <el-table-column
        prop="totalSeconds"
        label="答题时间(s)">
      </el-table-column>
      <el-table-column
        prop="accuracyRate"
        label="正确率(%)">
      </el-table-column>
      <el-table-column
        prop="userName"
        label="录入人">
      </el-table-column>
      <el-table-column
        prop="operation"
        label="操作">
        <template slot-scope="scope">
       <el-button type="danger" icon="el-icon-delete" circle plain @click="deleteable(scope.row)"></el-button>
       </template>
      </el-table-column>
    </el-table>
<!-- 分页区域 -->
 <el-pagination
      class="PagingAreas"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="queryInfo.page"
      :page-size="20"
      layout="prev, pager, next,sizes,jumper"
      :page-sizes="[20,30,50,100]"

      :total="180">
    </el-pagination>
 <!--题目预览区域  -->
      <el-dialog
        title="题目预览"
        :visible.sync="diaItenVisible"
        width="50%"
        v-if="diaItenVisible"
      >
        <questions-preview :id="rowId"></questions-preview>
        <span slot="footer" class="dialog-footer">
          <el-button @click="diaItenVisible = false" type="primary"
            >关闭</el-button
          >
        </span>
      </el-dialog>
</el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import QuestionsPreview from '../components/questions-preview'
export default {
  components: {
    QuestionsPreview
  },
  data () {
    return {
      input: '',
      queryInfo: {
        page: 1, // 当前页码大小
        pagesize: 10, // 页码大小
        keyword: '' // 关键字搜索
      },
      listItem: [], // 列表数据
      total: 0, // 总条数
      diaItenVisible: false
    }
  },
  created () {
    this.list()
  },
  methods: {
    // 获取数据
    async list () {
      try {
        const { data } = await randoms(this.queryInfo)
        // console.log(data);
        this.listItem = data.items
        // console.log(this.listItem)
        this.total = data.counts
      } catch (err) {
        this.$message('获取数据失败')
      }
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.list()
    },
    handleCurrentChange (newPagenum) {
      this.queryInfo.page = newPagenum
      this.list()
    },
    // 清除关键字
    clearKeyword () {
      this.queryInfo.keyword = ''
      this.list()
    },
    // 点击按钮删除当前列表项
    async deleteable (scope) {
      console.log(scope)
      const confirmResult = await this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      await removeRandoms({ id: scope.id })
      this.list()
      this.$message.success('删除成功')
    },
    getItemDialog (row) {
      this.diaItenVisible = true
      this.rowId = row.id
    }
  }
}
</script>

<style scoped lang='less'>
.container{
  .box-card{
    margin:10px;
  }
  .PagingAreas {
      display: flex;
      justify-content: flex-end;
      margin: 20px 0;
    }
}
  </style>
